<template>
  <div class="app-container" style="height:100%;">
    <div class="allRole">
      <div class="roleTop">
        <div class="itemTitle">
          <img src="@/assets/images/gzq/icon-xm.png" alt="">
          <span>消息中心</span>
        </div>
        <div class="addBtn" @click="handleAdd">
          <img src="@/assets/images/gzq/icon-add.png" alt="">
          <span>添加</span>
        </div>
      </div>
      <div class="roleList">
        <div :class="roleIndex == index ? 'active roleItem' : 'roleItem'" v-for="(item,index) in roleList" :key="index" @click="chengRole(index)">
          {{item.name}}
        </div>
      </div>
    </div>
    <div class="rightTable">
      <div class="tableTop">
        <div class="itemTitle">
          <img src="@/assets/images/gzq/icon-xm.png" alt="">
          <span>系统管理员</span>
        </div>
        <div>
          <el-input placeholder="请输入成员姓名进行搜索" prefix-icon="el-icon-search" v-model="form.name"></el-input>
        </div>
        <div class="tabList">
          <div :class="tabIndex == index ? 'active' : 'tabItem'" v-for="item,index in tabList" :key="index" @click="changeTab(index)">
             {{ item }}
          </div>
        </div>
      </div>
      <myTableSearch
        ref="myTableSearchRef"
        :isOperate="isOperate"
        :operateWidth="120"
        :titleName="$route.meta.title"
        :queryFn="getList"
        :formData="[]"
        :isIndex="false"
        :tableLabel="tabIndex == 0 ? tableLabel : tableData"
        @recombinationData="recombinationData"
      >
        <template slot="operate" slot-scope="scope">
          <div class="operateBtn flex justify-center">
            <div class="itemBtn" @click="handleUpdate">变换角色</div>
          </div>
        </template>
      </myTableSearch>
    </div>

    <myDialog :isDialog="open" :title="title" :width="'600px'" @handleClose="cancel">
      <template slot="content">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="更换角色" prop="ghName" v-if="title == '更换角色'">
            <el-select v-model="form.ghName" placeholder="请选择角色名称">
              <el-option label="一" value="shanghai"></el-option>
              <el-option label="二" value="beijing"></el-option>
            </el-select>
          </el-form-item>
          <div v-else>
            <el-form-item label="角色名称" prop="roleName" >
              <el-input v-model="form.name" placeholder="请输入角色名称"/>
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input type="textarea" v-model="form.desc" placeholder="请输入角色描述"></el-input>
            </el-form-item>
          </div>
        </el-form>
      </template>
      <template slot="footer">
        <div class="formBtnBox">
          <div class="editBtn "  @click="cancel">取消</div>
          <div class="saveBtn ml-4" @click="submitForm">保存</div>
        </div>
      </template>
    </myDialog>
  </div>
</template>

<script>
import {rolePage, roleDetail, roleDelete, roleAdd, roleUpdate, dataScope} from "@/api/sys/role";
import {treeSelect as menuTreeSelect, treeSelectMenuByRole} from "@/api/sys/menu";

export default {
  name: "Role",
  data() {
    return {
      roleIndex:'',
      roleList:[
        {
          name:'系统管理员（4）',
          num:'4'
        },{
          name:'安全管理员（1）'
        },{
          name:'安全审计员（1）'
        },{
          name:'用户自定义的角色'
        }
      ],
      tabIndex:'',
      tabList:['成员列表','权限范围'],
      form: {},
      isOperate:true,
      tableLabel: [
        {
          prop: "roleName",
          label: "成员姓名",
        },
        {
          prop: "roleKey",
          label: "所属机构",
        },
        {
          prop: "sort",
          label: "所属部门",
        },
      ],
      tableData: [
        {
          prop: "roleName",
          label: "角色名称",
        },
        {
          prop: "roleKey",
          label: "功能点",
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      // 日期范围
      dateRange: [],
      // 数据范围选项
      dataScopeOptions: [
        {
          value: "1",
          label: "全部数据权限"
        },
        {
          value: "2",
          label: "本部门及以下数据权限"
        },
        {
          value: "3",
          label: "本部门数据权限"
        },
        {
          value: "4",
          label: "仅本人数据权限"
        }
      ],
      // 菜单列表
      menuOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        query: {
          roleName: undefined,
          roleKey: undefined,
          status: undefined
        }
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        roleName: [
          {required: true, message: "角色名称不能为空", trigger: "blur"}
        ],
        ghName: [
          {required: true, message: "角色名称不能为空", trigger: "chenge"}
        ],
      }
    };
  },
  // created() {
  //   this.getList();
  // },
  methods: {
    chengRole(i){
      this.roleIndex = i
    },
    changeTab(i){
      this.tabIndex = i
      
      this.isOperate = !this.isOperate
      
    },
    getList(e) {
      return rolePage(e)
    },
    //格式化搜索字段
    recombinationData(param, callback) {
      param = {
        ...param,
        params:{}
      };
      callback({ ...param });
    },
    /** 查询角色列表 */
    // getList() {
    //   this.loading = true;
    //   rolePage(this.addDateRange(this.queryParams, this.dateRange)).then(
    //       response => {
    //         this.roleList = response.data.rows;
    //         this.total = response.data.total;
    //         this.loading = false;
    //       }
    //   );
    // },
    /** 查询菜单树结构 */
    getMenuTreeSelect() {
      menuTreeSelect({}).then(response => {
        this.menuOptions = response.data;
      });
    },
    // 所有菜单节点数据
    getMenuAllCheckedKeys() {
      // 目前被选中的菜单节点
      let checkedKeys = this.$refs.menu.getCheckedKeys();
      // 半选中的菜单节点
      let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    /** 根据角色ID查询菜单树结构 */
    getRoleMenuTreeSelect(roleId) {
      return treeSelectMenuByRole(roleId).then(response => {
        this.menuOptions = response.menus;
        return response;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      if (this.$refs.menu !== undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.menuExpand = false,
          this.menuNodeAll = false,
          this.form = {
            menuCheckStrictly: true,
            id: undefined,
            roleName: undefined,
            roleKey: undefined,
            sort: 0,
            menuIds: [],
            remark: undefined
          };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getMenuTreeSelect();
      this.open = true;
      this.title = "添加角色";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const roleId = '1';
      const roleMenu = this.getRoleMenuTreeSelect(roleId);
      roleDetail(roleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.$nextTick(() => {
          roleMenu.then(res => {
            this.$refs.menu.setCheckedKeys(res.checkedKeys);
          });
        });
        this.title = "更换角色";
      });
    },
    /** 分配数据权限操作 */
    handleDataScope(row) {
      this.reset();
      roleDetail(row.id).then(response => {
        this.form = response.data;
        this.openDataScope = true;
        this.title = "分配数据权限";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            this.form.menuIds = this.getMenuAllCheckedKeys();
            roleUpdate(this.form).then(() => {
              this.msgSuccess("修改成功");
              this.open = false;
              // this.getList();
              this.$refs.myTableSearchRef.searchReset();
            });
          } else {
            this.form.menuIds = this.getMenuAllCheckedKeys();
            roleAdd(this.form).then(() => {
              this.msgSuccess("新增成功");
              this.open = false;
              // this.getList();
              this.$refs.myTableSearchRef.searchReset();
            });
          }
        }
      });
    },
    /** 提交按钮（数据权限） */
    submitDataScope: function () {
      if (this.form.id !== undefined) {
        dataScope(this.form).then(() => {
          this.msgSuccess("修改成功");
          this.openDataScope = false;
          // this.getList();
          this.$refs.myTableSearchRef.searchReset();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除角色为"' + row.roleName + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return roleDelete(row.id);
      }).then(() => {
        // this.getList();
        this.$refs.myTableSearchRef.searchReset();
        this.msgSuccess("删除成功");
      })
    },
  }
};
</script>

<style scoped lang="scss">
.app-container{
  display: flex;
  align-items: flex-start;
}
.allRole{
  display: flex;
  flex-direction: column;
  padding: 16px;
  border: 1px solid #eff0f1;
  margin-right: 16px;
  width: 304px;
  height: 100%;
  .roleTop{
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-bottom: 11.5px;
    border-bottom: 1px solid #EFF0F1;
    .addBtn{
      padding: 3px 8px;
      border-radius: 32px;
      background: #1e6fff1a;
      display: flex;
      align-items: center;
      justify-content: center;
      >img{
        width: 16px;
        height: 16px;
      }
      >span{
        color: #1E6FFF;
        font-family: AlibabaPuHuiTi3.0-55Regular;
        font-size: 12px;
        white-space: nowrap;
        padding-left: 4px;
      }
    }
  }
  .roleList{
    display: flex;
    flex-direction: column;
    padding-top: 15.5px;
    .roleItem{
      padding: 9.5px 24px;
      margin-bottom: 8px;
      color: #464d5a;
      font-family: "Alibaba PuHuiTi 3.0";
      font-weight: 400;
      font-size: 15px;
      cursor: pointer;
    }
    .active{
      background: #f5f6fa;
    }
  }
}
.rightTable{
  flex: 1;
  width: 100%;
  .tableTop{
    display: flex;
    align-items: center;
    padding-bottom: 15.5px;
    border-bottom: 1px solid #EFF0F1;
    margin-bottom: 15.5px;
    >div:nth-child(2){
      margin-left: 80px;
    }
    .tabList{
      display: flex;
      align-items: center;
      flex: 1;
      justify-content: flex-end;
      .tabItem{
        padding: 15px 72px;
        color: #464d5a;
        font-family: "Alibaba PuHuiTi 3.0";
        font-weight: 400;
        font-size: 14px;
        margin-left: 16px;
        cursor: pointer;
      }
      .active{
        padding: 15px 72px;
        border-bottom: 1px solid #1e6fff;
        background: #1e6fff1a;
        color: #1e6fff;
        font-family: "Alibaba PuHuiTi 3.0";
        font-weight: 400;
        font-size: 14px;
        margin-left: 16px;
      }
    }
  }
}
.itemTitle{
  display: flex;
  align-items: center;
 
  
  >img{
    width: 12px;
    height: 12px;
  }
  >span{
    color: #464d5a;
    font-family: "Alibaba PuHuiTi 3.0";
    font-weight: 700;
    font-size: 16px;
    padding-left: 4px;
  }
}
/deep/.el-checkbox__label{
  color: #0db3ed !important;
}
.el-tree{
  // background: #08141e;
}

/deep/.el-textarea__inner{
  // background:#132C42 ;
  // color: #0db3ed !important;
  // border: none;
}
</style>
